require(tidyverse)

setwd('C:/Users/Jimbo/Dropbox/FED/FED/Paper/JOP/RR1_replication/')

raw <- read_csv('./data/yellen_tone_validation_numeric.csv')

svy <- read_lines('./MCSurvey.txt')

svy2 <- svy[which(grepl(' Q\\d+| \\[\\[Choice:',svy))]

lookup <- NULL
for(i in seq(1,297,by = 3)) {
  lookup <- lookup %>%
    bind_rows(data.frame(question = svy2[i],option1 = svy2[i+1],option2 = svy2[i+2])) %>%
    as_tibble()
}

svy
lookup <- lookup %>%
  mutate(question = trimws(gsub(':.*','',question)),
         option1 = as.numeric(str_extract(option1,'\\d+\\.\\d+')),
         option2 = as.numeric(str_extract(option2,'\\d+\\.\\d+')))

prepped <- raw %>%
  slice(-c(1,2)) %>%
  mutate(`Duration (in seconds)` = as.numeric(`Duration (in seconds)`)) %>%
  filter(`Duration (in seconds)` > 33) %>%
  select(StartDate,matches('QID')) %>%
  mutate_at(vars(matches('QID')),as.numeric) %>%
  gather(question,aggression,-StartDate) %>%
  spread(StartDate,aggression) %>%
  mutate(question = paste0('Q',row_number())) %>%
  gather(StartDate,aggression,-question)


prepped <- prepped %>%
  left_join(lookup %>%
              rowwise() %>%
              mutate(agg = max(option1,option2)) %>%
              ungroup())



prepped %>%
  mutate(diff = agg - aggression) %>%
  ggplot(aes(x = diff)) + 
  geom_histogram()

prepped %>%
  group_by(question) %>%
  filter(aggression == max(aggression)) %>%
  ggplot(aes(x = agg,y = aggression,color = StartDate)) + 
  geom_point()

prepped %>%
  mutate(correct = aggression == agg) %>%
  group_by(StartDate) %>%
  summarise(propCorrect = mean(correct,na.rm=T))

toPrint <- prepped %>%
  mutate(correct = aggression == agg) %>%
  group_by(question) %>%
  summarise(propCorrect = mean(correct,na.rm=T)) %>%
  filter(propCorrect == 0)

which(grepl(toPrint$question[1],svy))
svy[(which(grepl(toPrint$question[1],svy))+3):(which(grepl(toPrint$question[1],svy))+6)]

grabber <- function(ind) {
  svy[(ind+3):(ind+6)]
}

grabber(which(grepl(paste0(toPrint$question[9],':'),svy)))

which(grepl('Q22',svy))
svy[which(grepl('Q22',svy)):(which(grepl('Q22',svy))+6)]

which(grepl('Q22',svy))
svy[which(grepl('Q22',svy)):(which(grepl('Q22',svy))+6)]
